package com.lut.edu.apm.webCenter.controller;

import com.lut.edu.apm.webCenter.pojo.AjaxResult;
import com.lut.edu.apm.webCenter.pojo.TokenResponse;
import com.lut.edu.apm.webCenter.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Zhao Suzhen
 * @date `2024/6/4`
 */
@RestController
@RequestMapping("/manag")
public class LoginController {

    @Autowired
    UserService userService;

    @PostMapping("login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                              HttpServletResponse response) {
        TokenResponse token = (TokenResponse) userService.login(username, password).get("data");

        // 将 token 添加到响应头的 Authorization 中
        response.setHeader("Authorization", "Bearer " + token.getAccessToken());

        return token.getAccessToken();
    }
}
